package models

import (
	"fmt"
	"time"

	"github.com/beego/beego/v2/client/orm"
)

// TExamAnswer 考试答卷模型
type TExamAnswer struct {
	Id          int       `orm:"column(id);auto"`
	ExamId      int       `orm:"column(exam_id);null" description:"试卷id"`
	ExamTitle   string    `orm:"column(exam_title);size(145);null" description:"试卷标题"`
	StudentSn   string    `orm:"column(student_sn);null" description:"学生学号"`
	StudentName string    `orm:"column(student_name);size(45);null" description:"学生名字"`
	Content     string    `orm:"column(content);type(longtext);null" description:"答题内容"`
	Score       float64   `orm:"column(score);digits(10);decimals(2);null" description:"考试得分"`
	CreatedAt   time.Time `orm:"column(created_at);type(datetime);auto_now_add" description:"创建时间"`
	UpdatedAt   time.Time `orm:"column(updated_at);type(datetime);auto_now" description:"更新时间"`
	Analysis    string    `orm:"column(analysis);type(longtext);null" description:"点评"`
}

// TableName 设置表名
func (t *TExamAnswer) TableName() string {
	return "t_exam_answer"
}

// init 注册模型
func init() {
	orm.RegisterModel(new(TExamAnswer))
}

// AddTExamAnswer 添加考试答卷
func AddTExamAnswer(m *TExamAnswer) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(m)
	return
}

// GetTExamAnswerById 根据ID获取考试答卷
func GetTExamAnswerById(id int) (v *TExamAnswer, err error) {
	o := orm.NewOrm()
	v = &TExamAnswer{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

// GetAllTExamAnswer 获取所有考试答卷
func GetAllTExamAnswer() (result []*TExamAnswer, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(TExamAnswer))
	_, err = qs.All(&result)
	return
}

// UpdateTExamAnswer 更新考试答卷
func UpdateTExamAnswerById(m *TExamAnswer) (err error) {
	o := orm.NewOrm()
	v := TExamAnswer{Id: m.Id}
	// 确认记录存在
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteTExamAnswer 删除考试答卷
func DeleteTExamAnswer(id int) (err error) {
	o := orm.NewOrm()
	v := TExamAnswer{Id: id}
	// 确认记录存在
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&TExamAnswer{Id: id}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

// GetTExamAnswersByExamId 根据考试ID获取答卷列表
func GetTExamAnswersByExamId(examId int) (result []*TExamAnswer, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(TExamAnswer)).Filter("ExamId", examId)
	_, err = qs.All(&result)
	return
}

// GetTExamAnswersByStudentSn 根据学生学号获取答卷列表
func GetTExamAnswersByStudentSn(studentSn int) (result []*TExamAnswer, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(TExamAnswer)).Filter("StudentSn", studentSn)
	_, err = qs.All(&result)
	return
}

// GetTExamAnswerByStudentSnAndExamId 根据学生学号和考试ID获取答卷
func GetTExamAnswerByStudentSnAndExamId(studentSn string, examId int) (result *TExamAnswer, err error) {
	o := orm.NewOrm()
	result = &TExamAnswer{}
	qs := o.QueryTable(new(TExamAnswer)).Filter("StudentSn", studentSn).Filter("ExamId", examId)
	err = qs.One(result)
	return
}
